
import com.nstar.base.event.RequestEvent;
import com.nstar.base.util.EffectUtils;
import com.nstar.jianli.components.Toast;
import com.nstar.jianli.itemrenderer.JianLiRenderer;
import com.nstar.jianli.vo.JianLiVO;
import com.sina.microblog.data.MicroBlogUser;

import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.utils.ByteArray;

import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.graphics.ImageSnapshot;
import mx.graphics.codec.JPEGEncoder;
import mx.graphics.codec.PNGEncoder;


private const jpegEnc:JPEGEncoder = new JPEGEncoder(85); 
private const pngEnc:PNGEncoder = new PNGEncoder();
public static const CREATED_SNAPSHOT:String = "createdSnapshot";
public static const BLUE_SKIN:String = "blueSkin";
public static const PINK_SKIN:String = "pinkSkin";

private var appUrl:String = "http://wbtimeline.sinaapp.com";
private var urlRequset:URLRequest;
private var file:FileReference = new FileReference();
private var serverSide:String = "http://wbtimeline.sinaapp.com/uploader.php";
private var localPath:String = "";

var bmpDataCopy:BitmapData;
var bmpDataSrc:BitmapData;
var bmp:Bitmap;

public var skinName:String = "blueSkin";

private function init():void{
	btnAdd.addEventListener(MouseEvent.CLICK,addJianLiHandler);
	btnExport.addEventListener(MouseEvent.CLICK,exportHandler);
	btnReset.addEventListener(MouseEvent.CLICK,resetHandler);
	
	this.addEventListener(JianLiRenderer.REMOVE_JIANLI_ITEM,removeJianLiHandler);
	
//	avatar1.addEventListener(ProgressEvent.PROGRESS,loadAvatarHandler);
//	avatar1.addEventListener(Event.COMPLETE,loadAvatarHandler);
	
	file.addEventListener(Event.SELECT,onSelect);
	
	initWorkYears();
	
	
	txtName.addEventListener(FlexEvent.ENTER,enterHandler);
	txtInteresting.addEventListener(FlexEvent.ENTER,enterHandler);
	txtTarget.addEventListener(FlexEvent.ENTER,enterHandler);
	txtSkill.addEventListener(FlexEvent.ENTER,enterHandler);
	txtLinkNumber.addEventListener(FlexEvent.ENTER,enterHandler);
	txtMail.addEventListener(FlexEvent.ENTER,enterHandler);
	txtRemarks.addEventListener(FlexEvent.ENTER,enterHandler);
	
	EffectUtils.setFlowEffect(this,0x333333,5);
	
	txtName.setFocus();
	txtName.selectionBeginIndex = txtName.text.length;
	txtName.selectionEndIndex = txtName.text.length;
}

private function initWorkYears():void{
	var ary:Array = new Array();
	ary.push({data:0,label:"刚毕业"});
	ary.push({data:1,label:"1年"});
	ary.push({data:2,label:"2年"});
	ary.push({data:3,label:"3年"});
	ary.push({data:4,label:"4年"});
	ary.push({data:5,label:"5年"});
	ary.push({data:6,label:">5年"});
	ary.push({data:1,label:">10年"});
	workYears.dataProvider = ary;
}

public function update(user:MicroBlogUser):void{
	txtName.text = user.screenName;
	rbgGender.selectedValue = user.gender;
	txtRemarks.text = user.description;
}

private function enterHandler(event:FlexEvent):void{
	switch(event.target){
		case txtName:
			txtInteresting.setFocus();
			break;
		case txtInteresting:
			txtTarget.setFocus();
			break;
		case txtTarget:
			txtSkill.setFocus();
			break;
		case txtSkill:
			txtLinkNumber.setFocus();
			break;
		case txtLinkNumber:
			txtMail.setFocus();
			break;
		case txtMail:
			txtRemarks.setFocus();
			break;
		case txtRemarks:
			txtName.setFocus();
			break;
	}
}

public function doSelect(evt:Event):void{
       file.browse();
}

public function onSelect(evt:Event):void{
	localPath=file.name;
}

public function doUpload(event:Event):void{
	if(localPath==""){
		Alert.okLabel = "确 定";
		Alert.show("请选择要上传的文件！","警告"); 
	}else{
		var request:URLRequest=new URLRequest(serverSide); 
		file.addEventListener(Event.COMPLETE, uploadCompleteHandler); 
		file.addEventListener(ProgressEvent.PROGRESS,processHandler);
		file.upload(request);
		localPath = "上传中...";
	}

}

//下载进度
private function loadAvatarHandler(evt:Event):void{
	switch(evt.type){
		case ProgressEvent.PROGRESS:
			var proc:uint=ProgressEvent(evt).bytesLoaded/ProgressEvent(evt).bytesTotal*100;
			txtProgress.text = proc+"%";
			break;
		case Event.COMPLETE:
			bmp=Bitmap(evt.target.content);//类型转换
			bmp.width=135;	
			bmpDataSrc=bmp.bitmapData;//获取位图数据引用
			copyImage();
			break;
	}
}

//上传进度
public function processHandler(evt:ProgressEvent):void{
   var proc:uint=evt.bytesLoaded/evt.bytesTotal*100;
   localPath="上传进度:"+proc;
   txtProgress.text = proc+"%";
}

public function uploadCompleteHandler(event:Event):void{
	localPath = "上传成功的文件:"+event.target.name;
	var imageUrl:String = "http://wbtimeline-upload.stor.sinaapp.com/"+event.target.name;
	//avatar1.source = imageUrl;
	var loader:Loader = new Loader();
	loader.load(new URLRequest(imageUrl),new LoaderContext(true));
	loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadAvatarHandler);
	loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loadAvatarHandler);
	
	avatar1.addChild(loader);
}

public function enableOthen(visible:Boolean):void{
	formButtons.visible = formButtons.includeInLayout = visible;
	avatar1.visible = avatar1.includeInLayout = visible;
	txtTarget.showButton(visible);
	txtSkill.showButton(visible);
	txtInteresting.showButton(visible);
	txtLinkNumber.showButton(visible);
}

//验证必填的输入项是否已经填写
private function validate():Boolean{
	if(!txtName.name || !txtInteresting.text || !txtTarget.text 
		|| !txtSkill.text || !txtLinkNumber.text){
		Toast.show("请输入必填项~");
		return false;
	}
	return true;
}


private function exportHandler(event:MouseEvent):void{
	/* if(validate()==false){
		return;
	} */
	enableOthen(false);
	this.validateNow();
	var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(this, 0, pngEnc); 
	this.dispatchEvent(new RequestEvent(CREATED_SNAPSHOT,imageSnap.data as ByteArray));
	enableOthen(true);
}

private function resetHandler(event:MouseEvent):void{
	txtName.text = "";
	txtInteresting.text = "";
	txtTarget.text = "";
	txtSkill.text = "";
	txtLinkNumber.text = "";
	txtMail.text = "";
	txtRemarks.text = "";
	txtName.setFocus();
}



public function addJianLiHandler(event:MouseEvent):void{
//	var editor:JianLiEditor = JianLiEditor.show();
//	editor.addEventListener(JianLiEditor.CREATED_JIANLI,createdJianliHandler);
	var size1:int = listLeft.numChildren;
	var size2:int = listRight.numChildren;
	var vo:JianLiVO = new JianLiVO();
	if(skinName == Resume.BLUE_SKIN){
		vo.styleName = "jianLiFormBlue";
		vo.strokeColor = 0x36B0D9;
	}else if(skinName == Resume.PINK_SKIN){
		vo.styleName = "jianLiFormPink";
		vo.strokeColor = 0xE6108D;
	}
	
	if(size1>size2){
		vo.position = "right";
		listRight.addJianLi(vo);
	}else{
		vo.position = "left";
		listLeft.addJianLi(vo);
	}
	
	if(boxJianLi.visible == false)
		boxJianLi.visible = boxJianLi.includeInLayout = true;
}

private function createdJianliHandler(event:RequestEvent):void{
	var vo:JianLiVO = event.request as JianLiVO;
	
	var size1:int = listLeft.size();
	var size2:int = listRight.size();
	if(size1>size2){
		vo.position = "right";
		listRight.addJianLi(vo);
	}else{
		vo.position = "left";
		listLeft.addJianLi(vo);
	}
	
	if(boxJianLi.visible == false)
		boxJianLi.visible = boxJianLi.includeInLayout = true;
}

private function copyImage():void{
	bmpDataCopy=bmpDataSrc.clone();
	bmp=new Bitmap(bmpDataCopy);
	bmp.width=135;	
	//avatar2.source = bmp;
}


private function removeJianLiHandler(event:RequestEvent):void{
	if(listLeft.contains(event.request as DisplayObject)){
		listLeft.removeChild(event.request as DisplayObject);
	}else if(listRight.contains(event.request as DisplayObject)){
		listRight.removeChild(event.request as DisplayObject);
	}
}

public function changeBgColor(color:uint):void{
	jianliBack.setStyle("borderColor",color);
	jianliBack.setStyle("backgroundColor",color);
}